local ObjHuman = require("core.ObjHuman")
local Config = require("excel.Pet")
local PetConfig = Config.pet
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local ObjPet = require("core.ObjPet")
local Log = require("common.Log")
local PetLogic = require("pet.PetLogic")
local OfficialLogic = require("official.OfficialLogic")

--lv和exp只能传一个
function addLv(human, lv, exp)
    if not lv and not exp then
        return false
    end
    local petId = human.db.petCombatId
    if petId == 0 then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_NOT_HAVE_COMBAT_ID)
    end
    local data = ObjHuman.getPet(human, petId)
    if not data then
        return
    end
    local beforeLv = data.lv
    local petLvConfig = Config[PetConfig[petId].petName .. "Lv"]
    if lv then
        if data.lv >= human.db.lv then
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_LV_UP_ERR_CANNOT_UP)
            return false
        end
        data.lv = data.lv + lv
        if data.lv > human.db.lv then
            data.lv = human.db.lv
        end
    elseif exp then
        if data.lv == human.db.lv and data.exp == petLvConfig[data.lv].needExp then
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_LV_UP_ERR_CANNOT_UP)
            return false
        end
        data.exp = data.exp + exp
        while data.exp > petLvConfig[data.lv].needExp do
            if data.lv + 1 > human.db.lv then
                data.exp = petLvConfig[data.lv].needExp
                break
            else
                data.exp = data.exp - petLvConfig[data.lv].needExp
                data.lv = data.lv + 1
                Log.write(Log.LOGID_OSS_PET_LV, human._id, human.account, human.name, human.db.lv, data.id, data.lv, 2)
            end
        end
    end
    if beforeLv ~= data.lv then
      if human.db.petCombatId == data.id then
        ObjPet.update(human)
      end
      PetLogic.addAllRA(human)
      ObjHuman.doCalc(human)
      PetLogic.updatePet(human, petId)
    else
      PetLogic.updateExp(human, data.id)
    end
    OfficialLogic.checkOneCondition(human, "petLv")
    return true
end

--是否可使用道具
function canUseExpItem(human, cnt, exp, itemId)
    local id = human.db.petCombatId
    if id == 0 then
        Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_NOT_HAVE_COMBAT_ID)
        return false
    end

    local data = ObjHuman.getPet(human, id)
    local petLvConfig = Config[PetConfig[id].petName .. "Lv"]
    if data.lv > human.db.lv or (data.lv == human.db.lv and data.exp == petLvConfig[data.lv].needExp) then
        Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_LV_UP_ERR_CANNOT_UP)
        return false
    end

    if itemId then
        local tmpConfig = PetLogic.checkCanUp(itemId)
        local totalExp = 0
        local useCnt = 0
        local canUseExp = data.exp
        local canUseLv = data.lv
        for i=1, cnt do
            if canUseLv > human.db.lv or (canUseLv == human.db.lv and canUseExp == petLvConfig[data.lv].needExp) then
                Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_LV_UP_ERR_CANNOT_UP)
                break
            end
            local exp = tmpConfig.addUpVal
            canUseExp = canUseExp + exp
            totalExp = totalExp + exp
            while canUseExp > petLvConfig[canUseLv].needExp do
                if canUseLv+ 1 > human.db.lv then
                    canUseExp = petLvConfig[canUseLv].needExp
                    break
                else
                    canUseExp = canUseExp - petLvConfig[canUseLv].needExp
                    canUseLv = canUseLv + 1
                end
            end
            useCnt = useCnt + 1
        end
        return useCnt
    else
        local tempExp = 0
        for i = data.lv +1, human.db.lv do
            tempExp = tempExp + petLvConfig[i].needExp
        end
        return tempExp + petLvConfig[data.lv].needExp - data.exp
    end
end
--神将用道具加经验
function addExpItem2(human, id, itemId, cnt, isUse, useIndex)
    --  if isUse and human.db.petCombatId == 0 then
    --    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_NOT_HAVE_COMBAT_ID)
    --  end
    --  if isUse then
    id = human.db.petCombatId
    --  end
    local data = ObjHuman.getPet(human, id)
    if not data then
      return
    end
    local beforeLv = data.lv 
    local petLvConfig = Config[PetConfig[id].petName .. "Lv"]
    --  if data.lv > human.db.lv or (data.lv == human.db.lv and data.exp == petLvConfig[data.lv].needExp) then
    --    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_LV_UP_ERR_CANNOT_UP)
    --  end
    local tmpConfig = PetLogic.checkCanUp(itemId)
    --  if not tmpConfig then
    --    return
    --  end
    --  if BagLogic.getItemCount(human, itemId) < cnt then
    --    return
    --  end
    local totalExp = 0
    local useCnt = 0
    for i=1, cnt do
        if data.lv > human.db.lv or (data.lv == human.db.lv and data.exp == petLvConfig[data.lv].needExp) then
            Broadcast.sendBroadcast(human, Lang.DOWN, Lang.PET_LV_UP_ERR_CANNOT_UP)
            break
        end
        local exp = tmpConfig.addUpVal
        data.exp = data.exp + exp
        totalExp = totalExp + exp
        while data.exp > petLvConfig[data.lv].needExp do
            if data.lv + 1 > human.db.lv then
                data.exp = petLvConfig[data.lv].needExp
                break
            else
                data.exp = data.exp - petLvConfig[data.lv].needExp
                data.lv = data.lv + 1
                Log.write(Log.LOGID_OSS_PET_LV, human._id, human.account, human.name, human.db.lv, data.id, data.lv, 2)
            end
        end
        useCnt = useCnt + 1
    end

    Broadcast.sendBroadcast(human, Lang.DOWN, string.format(Lang.PET_LV_UP_SUCCESS, totalExp))
    if beforeLv ~= data.lv then
      if human.db.petCombatId == data.id then
        ObjPet.update(human)
      end
      PetLogic.addAllRA(human)
      ObjHuman.doCalc(human)
      PetLogic.updatePet(human, data.id)
    else
      PetLogic.updateExp(human, data.id)
    end
    OfficialLogic.checkOneCondition(human, "petLv")
    return true
end

function getCombatPetLv(human)
    local nCombatPetId = human.db.petCombatId
    if nCombatPetId > 0 then
        local nCombatPet = ObjHuman.getPet(human, nCombatPetId)
        if nCombatPet then
            return nCombatPet.lv
        end
        return nil
    end
    return -1
end

function addStar(human, id, star)
  local data = ObjHuman.getPet(human, id)
  if not data then
    local str = string.format(Lang.PET_NOT_HAVE, PetConfig[id].petMingzi)
    Broadcast.sendBroadcast(human, Lang.DOWN, str)
    return false
  end
  if data.star >= star then
    local str = string.format(Lang.PET_STAR_MORE_THAN, PetConfig[id].petMingzi, star)
    Broadcast.sendBroadcast(human, Lang.DOWN, str)
    return false
  end
  data.star = star
  PetLogic.addAllRA(human)
  ObjHuman.doCalc(human)
  PetLogic.updatePet(human, data.id)
    return true 
end
